package org.edu.modules.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.edu.modules.oa.entity.VeOaCarBaoXianEntity;
import org.edu.modules.oa.entity.VeOaCarInfoEntity;
import org.edu.modules.oa.mapper.IVeOaCarBaoXianMapper;
import org.edu.modules.oa.mapper.IVeOaCarInfoMapper;
import org.edu.modules.oa.service.IVeOaCarBaoXianService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;


@Service
public  class VeOaCarBaoXianServiceImp extends ServiceImpl<IVeOaCarBaoXianMapper,VeOaCarBaoXianEntity> implements IVeOaCarBaoXianService {
    @Resource
    private IVeOaCarBaoXianMapper iVeOaCarBaoXianMapper;
    @Resource
    private IVeOaCarInfoMapper veOaCarInfoMapper;

    public HashMap<String, Object> loadTable(Integer current, Integer pageSize,VeOaCarBaoXianEntity veOaCarBaoXianEntity) {
        System.out.println(current+"===="+pageSize);
        HashMap<String,Object> map = new HashMap<>();
        //创建分页的page
        IPage<VeOaCarBaoXianEntity> baoXianPage = new Page<>(current, pageSize);//参数一是当前页，参数二是每页个数
        //创建查询的条件
        QueryWrapper<VeOaCarBaoXianEntity> wrapper = new QueryWrapper<>();
        if(null != veOaCarBaoXianEntity.getCarName() && !veOaCarBaoXianEntity.getCarName().equals("")){
            wrapper.like("car_name", veOaCarBaoXianEntity.getCarName());
        }
        if(null != veOaCarBaoXianEntity.getCarId() && !veOaCarBaoXianEntity.getCarId().equals("")){
            wrapper.like("car_id", veOaCarBaoXianEntity.getCarId());
        }
        if(null != veOaCarBaoXianEntity.getFeiYongName() && !veOaCarBaoXianEntity.getFeiYongName().equals("")){
            wrapper.like("feiyong_name", veOaCarBaoXianEntity.getFeiYongName());
        }
        if(null != veOaCarBaoXianEntity.getFeiYongType() && !veOaCarBaoXianEntity.getFeiYongType().equals("")){
            wrapper.like("feiyong_type", veOaCarBaoXianEntity.getFeiYongType());
        }
        wrapper.orderByDesc("create_time");
        baoXianPage = iVeOaCarBaoXianMapper.selectPage(baoXianPage,wrapper);
        //获取数据
        List<VeOaCarBaoXianEntity> list = baoXianPage.getRecords();
        //统计页码
        Integer count = iVeOaCarBaoXianMapper.selectCount(wrapper);
        map.put("list",list);
        map.put("count", count);
        return map;
    }

    /*按id查看保险详情*/
    public VeOaCarBaoXianEntity selectInsureById(Integer id) {
        VeOaCarBaoXianEntity veOaCarBaoXianEntity = iVeOaCarBaoXianMapper.selectById(id);
        return veOaCarBaoXianEntity;
    }

    /*删除一条保险记录*/
    public int deleteInsureById(Integer id) {
        int res = iVeOaCarBaoXianMapper.deleteById(id);
        return res;
    }

    //查看全部的保险记录
    public List<VeOaCarBaoXianEntity> selectAllInsure() {
        QueryWrapper<VeOaCarBaoXianEntity> wrapper = new QueryWrapper<>();
        List<VeOaCarBaoXianEntity> list = iVeOaCarBaoXianMapper.selectList(wrapper);
        return list;
    }

    //修改保险记录的方法
    public int setInsureById(VeOaCarBaoXianEntity veOaCarBaoXianEntity) {
        int res = iVeOaCarBaoXianMapper.updateById(veOaCarBaoXianEntity);
        return res;
    }

    //新增保险记录
    public int addInsure(VeOaCarBaoXianEntity veOaCarBaoXianEntity) {
        int res = iVeOaCarBaoXianMapper.insert(veOaCarBaoXianEntity);
        return res;
    }

    //查看车辆信息
    public List<VeOaCarInfoEntity> selectCar() {
        QueryWrapper wrapper = new QueryWrapper();
        List<VeOaCarInfoEntity> list = veOaCarInfoMapper.selectList(wrapper);
        return list;
    }

    //查看车辆名称
    public VeOaCarInfoEntity selectCarById(Long id) {
        VeOaCarInfoEntity car = veOaCarInfoMapper.selectById(id);
        return car;
    }

    //查看车辆ID
    public VeOaCarInfoEntity selectCarId(String carName) {
        QueryWrapper<VeOaCarInfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("car_name", carName);
        VeOaCarInfoEntity car = veOaCarInfoMapper.selectOne(wrapper);
        return car;
    }

    //高级搜索
    public HashMap<String, Object> checkBaoXianEitity(String str) {
        List<VeOaCarBaoXianEntity> list = iVeOaCarBaoXianMapper.checkBaoXianEntity(str);
        int count = iVeOaCarBaoXianMapper.countBaoXianEntity(str);
        HashMap<String, Object> map = new HashMap<>();
        map.put("list",list);
        map.put("count",count);
        return map;
    }
    //查看当前车辆的保险记录
    public List<VeOaCarBaoXianEntity> selectInsureInportById(Integer carId) {
        QueryWrapper<VeOaCarBaoXianEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("car_id",carId);
        List<VeOaCarBaoXianEntity> list = iVeOaCarBaoXianMapper.selectList(wrapper);
        return list;
    }

}
